package com.lds.erp.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * description:
 *
 * @author saras_xu@163.com
 * @date 2018-10-12 17:22 创建
 */
public class TreeUtils {

    /**
     * @param list      简单结构数据
     * @param idCode    主键ID属性名称
     * @param pidCode   上级ID属性名称
     * @param childCode 子集属性名称
     * @return 包含子集的属性结构
     */
    public static String listToTree(List<?> list, String idCode, String pidCode, String childCode) {
        JSONArray listArr = JSONArray.parseArray(JSON.toJSONString(list));
        JSONArray resultJsonArr = new JSONArray();
        JSONObject hashJson = new JSONObject(true);
        //将数组转为Object的形式，key为数组中的id
        for (Object anArr : listArr) {
            JSONObject json = (JSONObject) anArr;
            hashJson.put(json.getString(idCode), json);
        }
        //遍历结果集
        for (Object anArr : listArr) {
            //单条记录
            JSONObject aVal = (JSONObject) anArr;
            //取出记录的pid值，并判断值是否为null，此处是为了避免pid.toString()空指针
            Object pid = aVal.get(pidCode);
            //在hash中取出key为单条记录中pid的值
            JSONObject hashVP = (JSONObject) hashJson.get(pid == null ? null : pid.toString());
            //如果记录的pid存在，则说明它有父节点，将她添加到孩子节点的集合中
            if (hashVP != null) {
                //检查是否有child属性
                JSONArray child;
                if (hashVP.get(childCode) != null) {
                    child = (JSONArray) hashVP.get(childCode);
                    child.add(aVal);
                    hashVP.put(childCode, child);
                } else {
                    child = new JSONArray();
                    child.add(aVal);
                    hashVP.put(childCode, child);
                }
            } else {
                resultJsonArr.add(aVal);
            }
        }
        return resultJsonArr.toJSONString();
    }

    public static void main(String[] args) {
        List<Map<String, String>> list = Lists.newArrayList();
        for (int i = 1; i <= 20; i++) {
            Map<String, String> data = Maps.newHashMap();
            data.put("id", "id" + i);
            data.put("name", "name" + i);
            if (i == 2 || i == 8 || i == 15) {
                data.put("pid", null);
            } else if (i == 3 || i == 14 || i == 17) {
                data.put("pid", "id2");
            } else if (i == 4 || i == 16) {
                data.put("pid", "id8");
            } else if (i == 6 || i == 11) {
                data.put("pid", "id15");
            } else if (i == 1 || i == 9) {
                data.put("pid", "id3");
            } else if (i == 5) {
                data.put("pid", "id14");
            } else if (i == 10) {
                data.put("pid", "id6");
            } else if (i == 18) {
                data.put("pid", "id9");
            } else if (i == 19) {
                data.put("pid", "id18");
            } else {
                data.put("pid", "id11");
            }
            list.add(data);
        }
        System.out.println(listToTree(list, "id", "pid", "child"));
    }

}
